package com.gitee.qdbp.general.system.biz.personnel.convert;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.gitee.qdbp.general.system.api.personnel.convert.IUserSourceConverter;
import com.gitee.qdbp.general.system.enums.IUserSource;

/**
 * 用户来源转换类
 *
 * @author zhaohuihua
 * @version 180116
 */
public class UserSourceHandler extends BaseTypeHandler<IUserSource> {

    private IUserSourceConverter converter;

    public UserSourceHandler(IUserSourceConverter converter) {
        this.converter = converter;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, IUserSource parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, String.valueOf(parameter.ordinal()));
    }

    @Override
    public IUserSource getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return toUserType(rs.getString(columnName));
    }

    @Override
    public IUserSource getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return toUserType(rs.getString(columnIndex));
    }

    @Override
    public IUserSource getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return toUserType(cs.getString(columnIndex));
    }

    private IUserSource toUserType(String string) {
        return converter.convert(string);
    }
}
